package apiexample.transform;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.sql.Connection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TransformController {
	private static int THREADS = 4;

	/**
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		try {
			PrintStream fos_err = new PrintStream(new File(
					"transform_process_multi.err"));
			PrintStream fos_out = new PrintStream(new File(
					"transform_process_multi.out"));
			System.setErr(fos_err);
			System.setOut(fos_out);

			Connection apisv_conn = com.apiexample.server.dao.BaseDao.getDbConnection("jdbc:mysql://localhost:3306/apiserverextdemo", "root", "");
			Connection apiemp_conn = dao.BaseDao.getDbConnection("jdbc:mysql://192.168.4.181:3306/apiexampletask4", "root", "woxnsk");
			CentralDataPool.persist(apiemp_conn, apisv_conn);
			
			System.out.println("End of persist!!!");
			ExecutorService exec = Executors.newFixedThreadPool(THREADS);
			// 为每个线程分配任务
			for (int j = 0; j < THREADS; ++j) {
				Connection apiexample_conn = dao.BaseDao.getDbConnection("jdbc:mysql://192.168.4.181:3306/apiexampletask4", "root", "woxnsk");
				Connection apiserver_conn = com.apiexample.server.dao.BaseDao.getDbConnection("jdbc:mysql://localhost:3306/apiserverextdemo", "root", "");
				PrintStream fos_err_thread = new PrintStream(new File(
				"transform_process_thread-"+j+".err"));
				PrintStream fos_out_thread = new PrintStream(new File(
				"transform_process_thread-"+j+".out"));
				TransformThread thread = new TransformThread(j,
						apiexample_conn, apiserver_conn, fos_out_thread, fos_err_thread);

				exec.execute(thread);
			}
			fos_err.flush();
			fos_err.close();
			fos_out.flush();
			fos_out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
